import { reactive } from "vue";
import { Api } from "../../../api/api.js";
import { ElNotification } from 'element-plus';
import { GJ_nowTime } from "../../../../static/js/tool.js";
function emailListJS() {
    let status = reactive({
        systemList: [],
        findData: {
            page: 1,
            total: null,
            limit: 10,
            pages: 1
        },
        userinfo: JSON.parse(window.sessionStorage.getItem("userinfo")),
        emailHide: false,
        emailDetails: {}
    });
    let methodFun = {
        getSysList() {
            let user = status.userinfo.user;
            Api.getSysList({ user }).then((res) => {
                status.systemList = res.data;
            });
        },
        //已读邮件
        readFun: (item) => {
            item.operaction = "read";
            item.isRead = true;
            console.log(item, 123);
            Api.changeMsg(item).then((res) => {
                if (res.data.acknowledged&&res.data.matchedCount>0) {
                    methodFun.getSysList();
                } else {
                    ElNotification({
                        type: "warning",
                        title: "网络异常",
                        message: "网络异常、请稍后再试！",
                        duration: 1000,
                    });
                }
            });
        },
        // 回复邮件
        replyFun: (item) => {
            console.log(item);
            ElNotification({
                type: "warning",
                title: "功能开发中",
                message: "回复功能敬请期待！",
                duration: 1000,
            });
        },
        //查看邮件
        lookFun: (item) => {
            methodFun.readFun(item);
            status.emailHide = true;
            status.emailDetails = item;
        },
        //删除邮件
        deleteFun: (item) => {
            if (item.isRead) {
                item.operaction = "delete";
                Api.changeMsg(item).then((res) => {
                    if (res.data) {
                        ElNotification({
                            type: "success",
                            title: "操作成功",
                            message: "该邮件已成功移除",
                            duration: 1000,
                        });
                        status.emailHide = false;
                        methodFun.getSysList();
                    }
                });
            } else {
                ElNotification({
                    type: "error",
                    title: "删除失败",
                    message: "无法删除未读邮件",
                    duration: 1000,
                });
            }
        },
        // 拒绝
        rejectFun: (item) => {
            item.status = 4;
            item.createTime = GJ_nowTime("date-time", "-");
            Api.delLove(item).then((res) => {
                if (res.data) {
                    methodFun.deleteFun(item);
                }
            })
        },
        //接受
        loveFun: (item) => {
            item.status = 3;
            item.createTime = GJ_nowTime("date-time", "-");
            Api.changeLove(item).then((res) => {
                console.log(res.data);
            });
            ElNotification({
                type: "success",
                title: "喜结良缘",
                message: "桃之夭夭，灼灼其华，之子于归，宜其室家",
                duration: 1000,
            });
        }

    }

    return {
        status,
        methodFun
    }
}

export default emailListJS;